package com.example.capstome;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
import com.squareup.picasso.Picasso;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
    private Context context;
    private List<CommentActivity.Comment> data;
    private DB_Helper dbHelper;

    public CommentAdapter(Context context, List<CommentActivity.Comment> list){
        super();
        this.context = context;
        this.data = list;
        dbHelper = new DB_Helper(context);
    }

    @SuppressLint("ResourceType")   // 抑制资源无效警告
    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //return new CommentViewHolder(LayoutInflater.from(context).inflate(R.id.content_item, parent, false));
        return new CommentViewHolder(LayoutInflater.from(context).inflate(R.layout.comment_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.content.setText(data.get(position).getContent());
        holder.time.setText(data.get(position).getTime());
        holder.userName.setText(data.get(position).getUserName());

        Picasso.get().load(data.get(position).getAvatarURL()).error(R.mipmap.ic_launcher_round)  .into(holder.avatar);

        holder.support.setText(data.get(position).getSupport());
        holder.support.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.hadSupported == false){
                    holder.hadSupported = true;
                    holder.support.setIconTint(ColorStateList.valueOf(Color.parseColor("#FF0000")));
                    dbHelper.addSupport(CommentActivity.getComment_id(position),
                            Integer.parseInt(holder.support.getText().toString()) + 1
                    );
                }
                else {
                    holder.hadSupported = false;
                    holder.support.setIconTint(ColorStateList.valueOf(Color.parseColor("#888888")));
                    dbHelper.addSupport(CommentActivity.getComment_id(position),
                            Integer.parseInt(holder.support.getText().toString()) - 1
                    );

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class CommentViewHolder extends RecyclerView.ViewHolder{
        CircleImageView avatar;
        TextView userName;
        TextView content;
        TextView time;
        MaterialButton support;
        boolean hadSupported;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.avatar_item);
            userName = itemView.findViewById(R.id.userName_item);
            content = itemView.findViewById(R.id.content_item);
            time = itemView.findViewById(R.id.time_item);
            support = itemView.findViewById(R.id.support_item);
            hadSupported = false;
        }
    }
}
